﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PIM.Common
{
    public static class MyExtensions
    {
        public static string ToSQL(this Op op)
        {
            switch (op)
            {
                case Op.EQ:
                    return "=";
                case Op.GT:
                    return ">";
                case Op.LT:
                    return "<";
                case Op.NE:
                    return "<>";
                case Op.LIKE:
                    return "LIKE";
            }
            throw new ApplicationException("Undefined Operator");
        }

        public static string ToQuotedValue(this string value)
        {
            return value.Replace("'", "''");
        }
    } 

    public enum Op
    {
        EQ,
        GT,
        LT,
        NE,
        LIKE
    }

    public class Predicate
    {
        public string Field;
        public string Value;
        public Op op;

        public override string ToString()
        {
            switch (this.op)
            {
                case Op.LIKE:
                    return string.Format("{0} {1} '{2}%'", Field, op.ToSQL(), Value.ToQuotedValue());
                default:
                    return string.Format("{0} {1} '{2}'", Field, op.ToSQL(), Value.ToQuotedValue());
            }
        }
    }

    public class Predicates : List<Predicate>
    {
    }
}
